package com.googlecode.player.controller
{
	
	import com.googlecode.interfaces.IAnimator3DPlayer;
	import com.googlecode.interfaces.IShell;
	import com.googlecode.player.Animator3DPlayer;
	import com.googlecode.player.core.Animator3DPlayerMediator;
	import com.googlecode.player.model.Animator3DPlayerProxy;
	import com.googlecode.player.view.viewport.ViewportMediator;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.command.SimpleCommand;
	
	/**
	 * Carries out PureMVC application startup. 
	 * @author Martin
	 */
	public class StartupCommand extends SimpleCommand
	{
		/**
		 * Execute the command.
		 * @param notification Body should contain a RevisedAnimator3D instance.
		 * @throws Error if incorrect body found in notification.
		 */		
		override public function execute(notification:INotification):void
		{
			var appRef:IShell = notification.getBody() as IShell;

			// test correct body
			if(appRef == null)
			{
				throw new Error("Notification body should implement IShell.");
			}

			// register proxy instances
			registerProxies();
			
			// register mediators
			// do this after the proxies so data is available
			registerMediators(appRef);	
		}
		
		/**
		 * Register PureMVC proxy instances. 
		 */	
		protected function registerProxies():void
		{
			facade.registerProxy(new Animator3DPlayerProxy());
		}
		
		
		protected function registerMediators(appRef:IShell):void
		{	
			var animator3DPlayer:IAnimator3DPlayer = appRef.animator3DPlayer;
			// Animator3DMediator listens for the viewport being added, so must be in place first
			facade.registerMediator(new Animator3DPlayerMediator(animator3DPlayer as Animator3DPlayer));
			
			
			facade.registerMediator(new ViewportMediator((animator3DPlayer as Animator3DPlayer).viewport));
			
			
			
			
		}
		
	}
}